package com.fantasy.servlet;

import com.fantasy.entity.Admin;
import com.fantasy.entity.Page;
import com.fantasy.entity.User;
import com.fantasy.service.UserService;
import com.fantasy.service.impl.UserServiceImpl;
import com.fantasy.utils.WebUtils;
import com.google.gson.Gson;


import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/user.do")
public class UserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();

    /**
     * 查询全部
     *
     * @param request
     * @param response
     */
    public void queryAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取数据库中的user
        List<User> users = userService.queryAll();
        System.out.println(users);
        //使用gson 将list转化为json对象
        Gson gson=new Gson();
        String userList = gson.toJson(users);
        //向前端发送json对象
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(userList);
    }

    /**
     * 添加用户
     * @param request
     * @param response
     * @throws IOException
     */
    public void addUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取前端参数
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        String headImg = request.getParameter("headImg");
        String email = request.getParameter("email");
        int state = Integer.parseInt(request.getParameter("state"));
        int vip = Integer.parseInt(request.getParameter("vip"));
        String registerDate = request.getParameter("registerDate");
        String introduce = request.getParameter("introduce");
        if (userService.queryByPhone(phone)==null){
            //将前端数据新建一个user
            User user=new User();
            user.setName(name);
            user.setPhone(phone);
            user.setPassword(password);
            user.setHeadImg(headImg);
            user.setEmail(email);
            user.setState(state);
            user.setVip(vip);
            user.setRegisterDate(registerDate);
            user.setIntroduce(introduce);
            int insert = userService.insert(user);
            response.setContentType("text/html;charset=utf-8");
            if (insert==1){
                response.getWriter().write("1");
            }else {
                response.getWriter().write("0");
            }
        }else {
            response.getWriter().write("-1");
        }

    }

    /**
     * 修改用户
     * @param request
     * @param response
     * @throws IOException
     */
    public void updateUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取前端参数
        int id =Integer.parseInt(request.getParameter("id")) ;
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        String headImg = request.getParameter("headImg");
        String email = request.getParameter("email");
        int state = Integer.parseInt(request.getParameter("state"));
        int vip = Integer.parseInt(request.getParameter("vip"));
        String registerDate = request.getParameter("registerDate");
        String introduce = request.getParameter("introduce");
        User user=new User();
        user.setId(id);
        user.setName(name);
        user.setPhone(phone);
        user.setPassword(password);
        user.setHeadImg(headImg);
        user.setEmail(email);
        user.setState(state);
        user.setVip(vip);
        user.setRegisterDate(registerDate);
        user.setIntroduce(introduce);
        int update = userService.update(user);
        response.setContentType("text/html;charset=utf-8");
        if (update==1){
            response.getWriter().write("1");
        }else {
            response.getWriter().write("0");
        }
    }

    /**
     * 删除user
     * @param request
     * @param response
     * @throws IOException
     */
    public void deleteUser(HttpServletRequest request,HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        int delete = userService.delete(id);
        response.setContentType("text/html;charset=utf-8");
        if (delete==1){
            response.getWriter().write("1");
        }else {
            response.getWriter().write("0");
        }
    }

    /**
     * 分页查询
     * @param request
     * @param response
     * @throws IOException
     */
    public void pageQuery(HttpServletRequest request,HttpServletResponse response) throws IOException {
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 0);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), Page.DEFAULT_PAGE_SIZE);

        Page<User> adminPage = userService.pageQuery(pageNo, pageSize);

        Gson gson=new Gson();
        String adminJson = gson.toJson(adminPage);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(adminJson);
    }
}
